package com.projectbox.utility.impl;

import java.io.*;
import java.util.Scanner;

import com.projectbox.utility.RuleUtility;

public class RuleUtilityImpl implements RuleUtility {
	
	//private final String fFileName = "/home/ec2-user/projectbox/rule.txt";
	private final String fFileName = "C:/projectbox/rule.txt";
	private final String fEncoding = "UTF-8";
	  
	public String getRule() throws IOException {
		StringBuilder text = new StringBuilder();
	    String NL = System.getProperty("line.separator");
	    Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
	    
	    try {
	      while (scanner.hasNextLine()){
	        text.append(scanner.nextLine() + NL);
	      }
	    }
	    finally{
	      scanner.close();
	    }
	    
	    return text.toString();
	}
	
	public void setRule(String rule) throws IOException {
		Writer out = new OutputStreamWriter(new FileOutputStream(fFileName), fEncoding);
		try {
			out.write(rule);
		} finally {
			out.close();
		}
	}
}
